package org.ncibi.cytoscape.metscape.network;

import org.ncibi.cytoscape.metscape.data.Attributes;
import org.ncibi.cytoscape.metscape.data.CompoundData;
import org.ncibi.cytoscape.metscape.utils.NumUtils;
import org.ncibi.metab.network.attribute.CompoundAttribute;
import org.ncibi.metab.network.node.CompoundNode;
import org.ncibi.metab.network.node.MetabolicNode;

public class CompoundNodeTranslator extends AbstractNodeTranslator implements NodeTranslator
{
	private CompoundData compoundData;
	
	public CompoundNodeTranslator(CompoundData compoundData) {
		this.compoundData = compoundData;
	}
	
    @Override
    protected String getCanonicalName(MetabolicNode node)
    {
        return CompoundNode.getName(node);
    }

    @Override
    protected void addAttributes(String id, MetabolicNode node)
    {
    	Attributes.node.setAttribute(id, CompoundAttribute.CID.toAttributeName(), CompoundNode.getCid(node));
        Attributes.node.setAttribute(id, CompoundAttribute.FORMULA.toAttributeName(),CompoundNode.getFormula(node));
        Attributes.node.setAttribute(id, CompoundAttribute.MOLECULAR_WEIGHT.toAttributeName(), CompoundNode.getMolecularWeight(node));
        Attributes.node.setAttribute(id, CompoundAttribute.APPROXIMATE_MOLECULAR_WEIGHT.toAttributeName(),CompoundNode.getApproximateMolecularWeight(node));
        Attributes.node.setAttribute(id, CompoundAttribute.NAME.toAttributeName(), CompoundNode.getName(node));
        Attributes.node.setAttribute(id, CompoundAttribute.SMILES.toAttributeName(), CompoundNode.getSmiles(node));
        Attributes.node.setAttribute(id, CompoundAttribute.CASNUM.toAttributeName(), CompoundNode.getCasnum(node));
        Attributes.node.setAttribute(id, CompoundAttribute.PUBCHEMCID.toAttributeName(), CompoundNode.getPubchemCid(node));
        Attributes.node.setAttribute(id, CompoundAttribute.BIOCYCID.toAttributeName(), CompoundNode.getBiocycId(node));
        Attributes.node.setAttribute(id, CompoundAttribute.CHEBIID.toAttributeName(), CompoundNode.getChebiId(node));
        Attributes.node.setAttribute(id, CompoundAttribute.HMDBID.toAttributeName(), CompoundNode.getHmdbId(node));
        Attributes.node.setListAttribute(id, CompoundAttribute.SYNONYMS.toAttributeName(), CompoundNode.getSynonyms(node));
        String name = compoundData.getName();
        if(compoundData.getData(id) != null) {
        	Double[] data = compoundData.getData(id);
        	String[] columns = compoundData.getColumns();
        	Attributes.node.setAttribute(id, "Compound." + name + ".hasData", true);
        	for (int i = 0; i < data.length; i++)
        	{
        		Attributes.node.setAttribute(id, "Compound." + name + "." + columns[i], NumUtils.truncate(data[i]));
        	}
        }
        else if(!compoundData.isEmpty()) {
        	Attributes.node.setAttribute(id, "Compound." + name + ".hasData", false);
        }

        // TODO: the MAINCOMPOUND and STOICHIOMETRY attributes are context dependent
        // they are associated with the reaction change to list? move to edge?
        // Attributes.node.setAttribute(id, prefix +
        // CompoundReactionAttribute.MAINCOMPOUND.toAttributeName(), CompoundNode
        // .isMainCompoundForReaction(node, rid));
        // Attributes.node.setAttribute(id, prefix +
        // CompoundReactionAttribute.STOICHIOMETRY.toAttributeName(), CompoundNode
        // .getStoichiometryForReaction(node, rid));
    }

}
